<template>
  <div class="upload-container">
    <el-upload :data="dataObj" name="file_single" :multiple="false" :show-file-list="true" :on-success="handleImageSuccess" class="image-uploader" :headers="headers" :limit="1" :on-preview="previewBtn" :file-list="fileList" :action="action">
      <el-button size="small" type="primary">点击上传</el-button>
    </el-upload>
  </div>
</template>

<script>
import { apipath } from '@/utils/config'
import { getToken } from '@/utils/auth'
export default {
  name: 'SingleImageUpload3',
  props: {
    value: {
      type: String,
      default: ''
    },
    fileUrl: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      accept: '',
      imageUrl: '',
      tempUrl: '',
      apipath: apipath,
      backinfo: [],
      fileList: [],
      action: '/admin/attachment/upload',
      headers: { token: '' }, // 请求头
      dataObj: { type: 'file' } // 请求其它参数
    }
  },
  mounted() {
    this.action = apipath + this.action
    this.headers.token = getToken()
    console.log(this.value)
    this.fileList = [{ url: apipath + this.fileUrl, name: 'PDF文件', uid: 1234125453 }]
  },
  methods: {
    selfUpload() {},
    rmImage() {
      this.emitInput('')
    },
    emitInput(val) {
      this.$emit('input', val)
    },
    zoomIn() {
      console.log('放大图片')
    },
    previewBtn(val) {
      console.log(val)
    },
    handleImageSuccess(file, res) {
      console.log(file)
      console.log(res)
      this.emitInput(file.data.url)
      this.backinfo = file.data
      this.imageUrl = file.data.view_url
    },
    beforeUpload() {
      // const _self = this
      return new Promise((resolve, reject) => {
        // getToken().then(response => {
        //   const key = response.data.qiniu_key
        //   const token = response.data.qiniu_token
        //   _self._data.dataObj.token = token
        //   _self._data.dataObj.key = key
        //   this.tempUrl = response.data.qiniu_url
        //   resolve(true)
        // }).catch(err => {
        //   console.log(err)
        //   reject(false)
        // })
      })
    }
  }
}
</script>
<style lang="scss" scoped>
@import '~@/styles/mixin.scss';
.upload-container {
  width: 100%;
  height: 50px;
  position: relative;
  @include clearfix;
  .image-uploader {
    width: 35%;
    float: left;
  }
  .image-preview {
    width: 100px;
    height: 100px;
    position: relative;
    border: 1px dashed #d9d9d9;
    float: left;
    // margin-left: 50px;
    .image-preview-wrapper {
      position: relative;
      width: 100%;
      height: 100%;
      img {
        cursor: pointer;
        width: 100%;
        height: 100%;
      }
    }
    .image-preview-action {
      position: absolute;
      width: 20px;
      height: 20px;
      left: 98px;
      top: 0;
      cursor: default;
      text-align: center;
      color: #fff;
      opacity: 0;
      font-size: 20px;
      background-color: rgba(0, 0, 0, 0.5);
      transition: opacity 0.3s;
      cursor: pointer;
      text-align: center;
      line-height: 18px;
      .el-icon-delete {
        font-size: 16px;
      }
    }
    &:hover {
      .image-preview-action {
        opacity: 1;
      }
    }
  }
  .image-app-preview {
    width: 320px;
    height: 180px;
    position: relative;
    border: 1px dashed #d9d9d9;
    float: left;
    margin-left: 50px;
    .app-fake-conver {
      height: 44px;
      position: absolute;
      width: 100%; // background: rgba(0, 0, 0, .1);
      text-align: center;
      line-height: 64px;
      color: #fff;
    }
  }
}
.upload-container {
  .el-upload {
    .el-upload-dragger {
      height: 50px;
    }
  }
}
</style>
